/*
 * read_rules.c
 *
 *  Created on: 2011-2-24
 *      Author: root
 */
#include "read_rules.h"

void read_rules_from_file()
{
	int n = 0;

	while(1)
	{
		if(n%6==0) //取rules文件里第1列
		{
			sip[n/6] = (char *)malloc(100);
			sprintf(sip[n/6],"%s",read_information("rules",',',n));
		}
		if(n%6==1) //取rules文件里第2列
		{
			dip[n/6] = (char *)malloc(100);
			sprintf(dip[n/6],"%s",read_information("rules",',',n));
		}
		if(n%6==2) //取rules文件里第3列
		{
			sport[n/6] = (char *)malloc(100);
			sprintf(sport[n/6],"%s",read_information("rules",',',n));
		}
		if(n%6==3) //取rules文件里第4列
		{
			dport[n/6] = (char *)malloc(100);
			sprintf(dport[n/6],"%s",read_information("rules",',',n));
		}
		if(n%6==4) //取rules文件里第5列
		{
			protocol[n/6] = (char *)malloc(100);
			sprintf(protocol[n/6],"%s",read_information("rules",',',n));
		}
		if(strlen(sip[n/6])==0) //文件到末尾，存储下来的字长为0
			break;
		++n;
	}

}

char *read_information(char *filename,char flag,int index)
{

	int i = 0,j = 0;
	FILE *fp;
	fp = fopen(filename,"r");
	char ch;
	while(j<index)
	{
		while((ch=fgetc(fp))!=flag&&!feof(fp));
			j++;
	}
	i=0;
	while((ch=fgetc(fp))!=flag&&!feof(fp))
	{
		if(ch!='\t' && ch!='\n')
		{
		text[i++]=ch;
		}
	}
	text[i++]='\0';

	return text;
	fclose(fp);
}
